#include <WiFi.h>
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
 #define SDA_PIN 2
#define SCL_PIN 3
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

const char* ssid     = "test";
const char* password = "12345678";

WiFiServer server(80);



void setup() {
  Serial.begin(115200);           //打开串口
 Wire.begin(SDA_PIN, SCL_PIN); // 指定SDA和SCL引脚
  pwm.begin();
  pwm.setPWMFreq(60); //伺服电机以60hz刷新率工作
  delay(1000);

  //设置PWM输出定时器
	// Allow allocation of all timers

  //设置Servo对象

  //连接wifi热点
  Serial.println("start now");
    // We start by connecting to a WiFi network
    Serial.print("Connecting to ");
    Serial.println(ssid);

    WiFi.begin(ssid, password);

    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }

    Serial.println("");
    Serial.println("WiFi connected.");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
    //启动web server
    server.begin();
}

void loop() {

   WiFiClient client = server.available();   // listen for incoming clients
 if (client) {                             // if you get a client,
    Serial.println("New Client.");           // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-Type: textml; charset=utf-8");
            client.println();

            // the content of the HTTP response follows the header:
           client.print("<div style='font-size:40px;text-align:center;margin:20px auto;'>");

            client.print("<style>");
            client.print("a.button {");
            client.print("  display: inline-block;");
            client.print("  background-color: #4CAF50;");  // 绿色按钮背景色
            client.print("  border: none;");
            client.print("  color: white;");
            client.print("  text-align: center;");
            client.print("  text-decoration: none;");
            client.print("  font-size: 48px;");  // 增大按钮字体大小
            client.print("  margin: 20px 10px;");  // 调整按钮之间的水平间距
            client.print("  padding: 30px 60px;");  // 增大按钮内边距
            client.print("  width: auto;");  // 自适应按钮宽度
            client.print("}");
            client.print("</style>");

            client.print("<a href=\"/forward\" class=\"button\">前</a>");
            client.print("<a href=\"/backward\" class=\"button\">后</a>");

            client.print("<br/><br/>");

            client.print("<a href=\"/left\" class=\"button\">左</a>");
            client.print("<a href=\"/right\" class=\"button\">右</a>");

            client.print("<br/><br/>");

            client.print("<a href=\"/greet\" class=\"button\">你好</a>");
            client.print("<a href=\"/sleep\" class=\"button\">睡觉</a>");

            client.print("<br/><br/>");

            client.print("</div>");

            // The HTTP response ends with another blank line:
            client.println();
            // break out of the while loop:
            break;
          } else {    // if you got a newline, then clear currentLine:
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }
        if (currentLine.endsWith("/forward")) {
		         Serial.println("你们");
             normal();
              forward();
              forward();
              forward();
              forward();
        }
        if (currentLine.endsWith("/backward")) {
		         
              normal();
              backward();
              backward();
              backward();
              backward();

        }
        if (currentLine.endsWith("/left")) {
		        
             normal();
            left();
            left();

        }
                if (currentLine.endsWith("/right")) {
		       
            normal();
            right();
            right();
        }
        if (currentLine.endsWith("/greet")) {
		        
              normal();
              hello();
        }
        if (currentLine.endsWith("/sleep")) {
		      
             sleep();
        }
      }
    }
    // close the connection:
    client.stop();
    Serial.println("Client Disconnected.");
  }

}

void sleep() 
{
  pwm.setPWM(4, 0, 460);
  delay(100);
  pwm.setPWM(5, 0, 460);
  delay(100);
  pwm.setPWM(6, 0, 460);
  delay(100);
  pwm.setPWM(7, 0, 460);
  delay(100);
  pwm.setPWM(0, 0, 678.9);
  delay(100);
  pwm.setPWM(1, 0, 200);
  delay(100);
  pwm.setPWM(2, 0, 200);
  delay(100);
  pwm.setPWM(3, 0, 678.9);
}
 
void normal() 
{
  pwm.setPWM(0, 0, 590);
  delay(100);
  pwm.setPWM(1, 0, 330);
  delay(100);
  pwm.setPWM(2, 0, 330);
  delay(100);
  pwm.setPWM(3, 0, 590);
  delay(100);
  pwm.setPWM(4, 0, 678.9);
  delay(100);
  pwm.setPWM(7, 0, 200);
  delay(100);
  pwm.setPWM(6, 0, 678.9);
  delay(100);
  pwm.setPWM(5, 0, 200);
}
 
void left()
{
  pwm.setPWM(4, 0, 590);
  delay(100);
  pwm.setPWM(0, 0, 678.9);
  delay(100);
  pwm.setPWM(4, 0, 678.9);
  delay(100);
  pwm.setPWM(6, 0, 590);
  delay(100);
  pwm.setPWM(2, 0, 460);
  delay(100);
  pwm.setPWM(6, 0, 678.9);
  delay(100);
  pwm.setPWM(7, 0, 330);
  delay(100);
  pwm.setPWM(3, 0, 678.9);
  delay(100);
  pwm.setPWM(7, 0, 200);
  delay(100);
  pwm.setPWM(5, 0, 330);
  delay(100);
  pwm.setPWM(1, 0, 460);
  delay(100);
  pwm.setPWM(5, 0, 200);
  delay(100);
  pwm.setPWM(0, 0, 590);
  pwm.setPWM(2, 0, 330);
  pwm.setPWM(3, 0, 590);
  pwm.setPWM(1, 0, 330);
}
 
void right()
{
  pwm.setPWM(5, 0, 330);
  delay(100);
  pwm.setPWM(1, 0, 200);
  delay(100);
  pwm.setPWM(5, 0, 200);
  delay(100);
  pwm.setPWM(7, 0, 330);
  delay(100);
  pwm.setPWM(3, 0, 460);
  delay(100);
  pwm.setPWM(7, 0, 200);
  delay(100);
  pwm.setPWM(6, 0, 590);
  delay(100);
  pwm.setPWM(2, 0, 200);
  delay(100);
  pwm.setPWM(6, 0, 678.9);
  delay(100);
  pwm.setPWM(4, 0, 590);
  delay(100);
  pwm.setPWM(0, 0, 330);
  delay(100);
  pwm.setPWM(4, 0, 678.9);
  delay(100);
  pwm.setPWM(1, 0, 330);
  pwm.setPWM(3, 0, 590);
  pwm.setPWM(2, 0, 330);
  pwm.setPWM(0, 0, 590);
}
 
void forward()
{ 
  pwm.setPWM(5, 0, 330);
  delay(50);
  pwm.setPWM(1, 0, 200);
  delay(50);
  pwm.setPWM(5, 0, 200);
  delay(50);
  pwm.setPWM(4, 0, 590);
  delay(50);
  pwm.setPWM(0, 0, 678.9);
  delay(50);
  pwm.setPWM(4, 0, 678.9);
  delay(50);
  pwm.setPWM(6, 0, 590);
  delay(50);
  pwm.setPWM(2, 0, 460);
  delay(50);
  pwm.setPWM(6, 0, 678.9);
  delay(50);
  pwm.setPWM(7, 0, 330);
  delay(50);
  pwm.setPWM(3, 0, 460);
  delay(50);
  pwm.setPWM(7, 0, 200);
  delay(50);
  pwm.setPWM(0, 0, 590);
  pwm.setPWM(1, 0, 330);
  pwm.setPWM(2, 0, 330);
  pwm.setPWM(3, 0, 590);
}
 
void backward()
{ 
  pwm.setPWM(7, 0, 330);
  delay(50);
  pwm.setPWM(3, 0, 678.9);
  delay(50);
  pwm.setPWM(7, 0, 200);
  delay(50);
  pwm.setPWM(6, 0, 590);
  delay(50);
  pwm.setPWM(2, 0, 200);
  delay(50);
  pwm.setPWM(6, 0, 678.9);
  delay(50);
  pwm.setPWM(5, 0, 330);
  delay(50);
  pwm.setPWM(1, 0, 460);
  delay(50);
  pwm.setPWM(5, 0, 200);
  delay(50);
  pwm.setPWM(4, 0, 590);
  delay(50);
  pwm.setPWM(0, 0, 460);
  delay(50);
  pwm.setPWM(4, 0, 678.9);
  delay(50);
  pwm.setPWM(3, 0, 590);
  pwm.setPWM(2, 0, 330);
  pwm.setPWM(1, 0, 330);
  pwm.setPWM(0, 0, 590);
}
 
void hello()
{
  pwm.setPWM(0, 0, 678.9);
  delay(100);
  pwm.setPWM(4, 0, 200);
  delay(500);
  pwm.setPWM(4, 0, 330);
  delay(500);
  pwm.setPWM(4, 0, 200);
  delay(500);
  pwm.setPWM(4, 0, 330);
  delay(500);
  pwm.setPWM(4, 0, 200);
  delay(500);
  pwm.setPWM(4, 0, 678.9);
}
 
void come()
{
  pwm.setPWM(0, 0, 460);
  delay(100);
  pwm.setPWM(4, 0, 200);
  delay(500);
  pwm.setPWM(4, 0, 330);
  delay(500);
  pwm.setPWM(4, 0, 200);
  delay(500);
  pwm.setPWM(4, 0, 330);
  delay(500);
  pwm.setPWM(4, 0, 200);
  delay(500);
  pwm.setPWM(4, 0, 678.9);
}